﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ParserGenerator.ParserConstruction
{
    [Serializable]
    public sealed class ParserAutomatonReductionLookaheadSetsTable : ReadOnlyDictionary<ParserAutomatonState, ParserAutomatonReductionLookaheadSets>
    {
        internal ParserAutomatonReductionLookaheadSetsTable()
            : base(new Dictionary<ParserAutomatonState, ParserAutomatonReductionLookaheadSets>())
        {
        }

        internal ParserAutomatonReductionLookaheadSetsTable(ParserAutomatonStateTable states)
            : this()
        {
            if (states == null)
                throw new ArgumentNullException(nameof(states));
            foreach (ParserAutomatonState state in states)
            {
                if (state.IsConsistent)
                    continue;
                var sets = new ParserAutomatonReductionLookaheadSets(state);
                Dictionary.Add(state, sets);
            }
        }

        internal void Add(ParserAutomatonState state, ParserAutomatonReductionLookaheadSets sets)
        {
            if (state == null)
                throw new ArgumentNullException(nameof(state));
            if (sets == null)
                throw new ArgumentNullException(nameof(sets));
            Dictionary.Add(state, sets);
        }
    }
}
